﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-08-14 15:44:15
 * 功能：三方购买
**************************************************************************************/

using Game.Framework;
using Game.Framework.Utility;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text;

public class Three : Purchase
{
    protected override void Start()
    {
        base.Start();
        m_IsoCurrencyCode = m_Cfg.currency;
    }

    private void OnApplicationPause(bool pause)
    {
        if (pause) return;
        RequestOrders();
    }

    public void RequestOrders()
    {
        var reqData = new CommonMono.NetPostData();
        reqData.url = m_Cfg.th_u;
        reqData.reqParams = new JObject
        {
            {"uid", GameDataMgr.Instance.Uid }
        };
        reqData.reqHeaders = new JObject()
        {
            {"Content-Type", "application/json" },
            {"Product", GlobalFixedString.PRODUCT_NAME },
        };
        reqData.callBack = ReqOrdersSuccHandler;
        reqData.failCallBack = ReqOrdersFailHandler;
        reqData.isEncrypt = true;
        CommonMono.Instance.SendPostReqest(reqData);
    }

    private void ReqOrdersSuccHandler(byte[] data)
    {
        string str = CryptoUtils.AesDecrypt(Encoding.UTF8.GetString(data));
        // 解析 JSON 并访问 status 字段
        var jsonResponse = JObject.Parse(str);
        int code = jsonResponse["code"].Value<int>();
        LogManager.Log($"response:{str}", "ReqOrdersSuccHandler");
        if (code != 1) return;
        var resultArr = (JArray)jsonResponse["data"];
        foreach (var item in resultArr)
        {
            string status = item["status"].ToString();
            string coid = item["coid"].ToString();
            //var pd = GameDataMgr.Instance.GetPDItem(coid.ToString());
            //if (pd == null) continue;
            //switch (status)
            //{
            //    case "20":
            //        EventCenter.Instance.Trigger(GameEventName.SUCC_BUY, pd.productName, pd.data);
            //        string id = pd.productName;
            //        var product = m_Cfg.products.Single(item => item.id == id);
            //        if (product != null && product.type == ProductType.NonConsumable)
            //            GameDataMgr.Instance.AddPurchasedNonConsumableItem(id);
            //        ReportBI(id, id, 1, 0, m_IsoCurrencyCode, "", "paid", "");
            //        GameDataMgr.Instance.UpdatePDItem(coid, true);
            //        break;
            //    case "60":
            //        EventCenter.Instance.Trigger(GameEventName.BUY_FAILED, pd.productName);
            //        ReportBI(pd.productName, pd.productName, 1, 0, m_IsoCurrencyCode, "", "paid_err", "");
            //        break;
            //    case "70":
            //        EventCenter.Instance.Trigger(GameEventName.BUY_FAILED, pd.productName);
            //        break;
            //}
        }
    }

    private void ReqOrdersFailHandler(string err)
    {
        LogManager.LogError(err, "Purchase", "ReqOrdersFailHandler");
    }

    public override void Buy(string id, object data)
    {
        base.Buy(id, data);

        //var product = BaseDataMgr.Instance.GetProductInfo(id);
        //ReportBI(id, id, 1, 0, m_IsoCurrencyCode, "", "order", "");
        //m_Data = data;

        //var url = product.products[1];
        //var pid = Guid.NewGuid().ToString();
        //url = url.Replace("{amount}", product.amount.ToString());
        //url = url.Replace("{goods_id}", id);
        //url = url.Replace("{goods_name}", id);
        //url = url.Replace("{uid}", GameDataMgr.Instance.Uid.ToString());
        //url = url.Replace("{coid}", pid);

        //// 保存订单
        //GameDataMgr.Instance.AddPDItem(pid, id, data);

        //// 打开Three
        //UIUtility.DisplayPanel(ThreeAnimPanelView.MEDIATOR_NAME, "UI/ThreeAnimPanel/Prefabs/ThreeAnimPanel", UIRoot.Top, url);
    }
}